<template>
    <!-- 对话框表单 -->
    <!-- 建议使用 Prettier 格式化代码 -->
    <!-- el-form 内可以混用 el-form-item、FormItem、ba-input 等输入组件 -->
    <el-dialog
        class="ba-operate-dialog"
        :close-on-click-modal="false"
        :model-value="['Add', 'Edit'].includes(baTable.form.operate!)"
        @close="baTable.toggleForm"
        width="60%"
        style="height: 80%"
    >
        <template #header>
            <div class="title" v-drag="['.ba-operate-dialog', '.el-dialog__header']" v-zoom="'.ba-operate-dialog'">
                {{ baTable.form.operate ? t(baTable.form.operate) : '' }}
            </div>
        </template>
        <el-scrollbar v-loading="baTable.form.loading" class="ba-table-form-scrollbar">
            <div
                class="ba-operate-form"
                :class="'ba-' + baTable.form.operate + '-form'"
                :style="config.layout.shrink ? '':'width: calc(100% - ' + baTable.form.labelWidth! / 2 + 'px)'"
            >
                <el-form
                    v-if="!baTable.form.loading"
                    ref="formRef"
                    @submit.prevent=""
                    @keyup.enter="baTable.onSubmit(formRef)"
                    :model="baTable.form.items"
                    :label-position="config.layout.shrink ? 'top' : 'right'"
                    :label-width="baTable.form.labelWidth + 'px'"
                    :rules="rules"
                >
                    <FormItem :label="t('scenic.string')" type="string" v-model="baTable.form.items!.name" prop="name"
                              :placeholder="t('Please input field', { field: t('scenic.string') })"/>
                    <FormItem label="分类" type="remoteSelect"
                              v-model="baTable.form.items!.cate_id" prop="cate_id"
                              :input-attr="{ pk: 'ba_cate.id', field: 'name', remoteUrl: '/admin/cate/index' }"
                              placeholder="请选择分类"/>
                    <FormItem :label="t('scenic.cover')" type="image" v-model="baTable.form.items!.cover" prop="cover"/>
<!--                    <FormItem :label="t('scenic.tags')" type="string" v-model="baTable.form.items!.tags" prop="tags"-->
<!--                              :placeholder="t('Please input field', { field: t('scenic.tags') })"/>-->
                    <FormItem :label="t('scenic.banner')" type="images" v-model="baTable.form.items!.banner"
                              prop="banner"/>



                    <FormItem label="每天限制数量" type="number" v-model="baTable.form.items!.limit_nums" prop="limit_nums"
                              placeholder="每天限制数量，0代表无限制"/>

                    <FormItem label="15天内限制数量" type="number" v-model="baTable.form.items!.fifteen" prop="fifteen"
                              placeholder="每天限制数量，0代表无限制"/>

                    <FormItem label="所属会员卡" type="remoteSelects"
                              v-model="baTable.form.items!.vip_card_ids" prop="vip_card_ids"
                              :input-attr="{ pk: 'ba_vip_card.id', field: 'name', remoteUrl: '/admin/vip.card/index',params:{'type':1} }"
                              placeholder="请选择所属会员卡"/>

                    <FormItem label="原价价格" type="string" v-model="baTable.form.items!.normal_price" prop="normal_price"
                              placeholder="请输入原价价格"/>

                    <FormItem label="会员价格" type="string" v-model="baTable.form.items!.vip_price" prop="vip_price"
                              placeholder="请输入会员价格"/>

                    <FormItem label="所属城市" type="city" :input-attr="{level:2}" v-model="baTable.form.items!.city" prop="city"/>


<!--                    <FormItem label="所属省份" type="remoteSelect"-->
<!--                              v-model="baTable.form.items!.province_name" prop="province_name"-->
<!--                              :input-attr="{ pk: 'ba_area.name', field: 'name', remoteUrl: '/admin/area/index?limit=34',pagination:false }"-->
<!--                              placeholder="请选择所属省份"/>-->

<!--                    <FormItem :label="t('scenic.city_name')" type="string" v-model="baTable.form.items!.city_name"-->
<!--                              prop="city_name"-->
<!--                              :placeholder="t('Please input field', { field: t('scenic.city_name') })"/>-->


                    <el-form-item :label="t('scenic.address')">
                        <el-input
                            type="string"
                            v-model="baTable.form.items!.address"
                            prop="address"
                            :placeholder="t('scenic.address')"
                            readonly
                        >
                            <template #append>
                                <el-button @click="openMapPickPop">选择地点</el-button>
                            </template>
                        </el-input>
                    </el-form-item>

                    <el-row :gutter="10">
                        <el-col :span="12">
                            <FormItem
                                :label="t('scenic.longitude')"
                                type="string"
                                v-model="baTable.form.items!.longitude"
                                prop="longitude"
                                :input-attr="{ readonly: true }"
                                :placeholder="t('Please input field', { field: t('scenic.longitude') })"
                            />
                        </el-col>
                        <el-col :span="12">
                            <FormItem
                                :label="t('scenic.latitude')"
                                type="string"
                                v-model="baTable.form.items!.latitude"
                                prop="latitude"
                                :input-attr="{ readonly: true }"
                                :placeholder="t('Please input field', { field: t('scenic.latitude') })"
                            />
                        </el-col>
                    </el-row>

                    <!--                    <FormItem :label="t('scenic.address')" type="string" v-model="baTable.form.items!.address"-->
                    <!--                              prop="address" :placeholder="t('Please input field', { field: t('scenic.address') })"/>-->
                    <FormItem :label="t('scenic.openTime')" type="string" v-model="baTable.form.items!.openTime"
                              prop="openTime" :placeholder="t('Please input field', { field: t('scenic.openTime') })"/>

                    <FormItem :label="t('排序')" type="number" prop="weigh" :input-attr="{ step: 1 }" v-model.number="baTable.form.items!.weigh" :placeholder="t('Please input field', { field: t('排序') })" />


                    <FormItem label="状态" type="radio" v-model="baTable.form.items!.status"
                              prop="status"
                              :input-attr="{ content: { '0': '禁用', '1': '启用' } }"
                              :placeholder="t('Please select field', { field: t('member.status') })"/>

                    <FormItem :label="t('scenic.traffic_bus')" type="editor" v-model="baTable.form.items!.traffic_bus"
                              prop="traffic_bus" @keyup.enter.stop="" @keyup.ctrl.enter="baTable.onSubmit(formRef)"
                              :placeholder="t('Please input field', { field: t('scenic.traffic_bus') })"/>
                    <FormItem :label="t('scenic.scenic_des')" type="editor" v-model="baTable.form.items!.scenic_des"
                              prop="scenic_des" @keyup.enter.stop="" @keyup.ctrl.enter="baTable.onSubmit(formRef)"
                              :placeholder="t('Please input field', { field: t('scenic.scenic_des') })"/>
                    <FormItem :label="t('scenic.book_notice')" type="editor" v-model="baTable.form.items!.book_notice"
                              prop="book_notice" @keyup.enter.stop="" @keyup.ctrl.enter="baTable.onSubmit(formRef)"
                              :placeholder="t('Please input field', { field: t('scenic.book_notice') })"/>

                    <FormItem label="备注" type="textarea" v-model="baTable.form.items!.notes"
                              :input-attr="{ showWordLimit: false,  rows: 6 }"/>


                    <!--                    <FormItem :label="t('scenic.latitude')" type="string" v-model="baTable.form.items!.latitude"-->
                    <!--                              prop="latitude" :placeholder="t('Please input field', { field: t('scenic.latitude') })"/>-->
                    <!--                    <FormItem :label="t('scenic.longitude')" type="string" v-model="baTable.form.items!.longitude"-->
                    <!--                              prop="longitude"-->
                    <!--                              :placeholder="t('Please input field', { field: t('scenic.longitude') })"/>-->
                </el-form>
            </div>
        </el-scrollbar>
        <template #footer>
            <div :style="'width: calc(100% - ' + baTable.form.labelWidth! / 1.8 + 'px)'">
                <el-button @click="baTable.toggleForm()">{{ t('Cancel') }}</el-button>
                <el-button v-blur :loading="baTable.form.submitLoading" @click="baTable.onSubmit(formRef)"
                           type="primary">
                    {{
                        baTable.form.operateIds && baTable.form.operateIds.length > 1 ? t('Save and edit next item') : t('Save')
                    }}
                </el-button>
            </div>
        </template>
    </el-dialog>
</template>

<script setup lang="ts">
import type {FormInstance, FormItemRule} from 'element-plus'
import {inject, reactive, ref} from 'vue'
import {useI18n} from 'vue-i18n'
import FormItem from '/@/components/formItem/index.vue'
import {useConfig} from '/@/stores/config'
import type baTableClass from '/@/utils/baTable'
import {buildValidatorData} from '/@/utils/validate'
import {getTencentmapKey} from "/@/api/backend/xmmap/tencentmap";
import {openTencentMapPickPop} from "/@/components/xmmap/tencentMap";
import {ElNotification} from 'element-plus'


const config = useConfig()
const formRef = ref<FormInstance>()
const baTable = inject('baTable') as baTableClass

const {t} = useI18n()


const state = reactive({
    tencentMapKey: '',
})
// 获取腾讯地图key，可替换自己的接口
getTencentmapKey().then((res) => {
    state.tencentMapKey = res.data
})
const openMapPickPop = () => {
    if (state.tencentMapKey == '') {
        ElNotification({
            type: 'error',
            message: '请前往【系统配置】【第三方密钥】配置腾讯地图KEY',
        })
        return false
    }
    openTencentMapPickPop(state.tencentMapKey)
}
window.addEventListener('message', function (event) {
    if (event.data.type == 'tencent-map-data') {
        const currentData = JSON.parse(event.data.data)
        console.log('腾讯地图当前位置数据：', currentData)
        baTable.form.items!.poiname = currentData.poiname
        baTable.form.items!.address = currentData.poiaddress
        baTable.form.items!.longitude = currentData.latlng.lng
        baTable.form.items!.latitude = currentData.latlng.lat
    }
})


const rules: Partial<Record<string, FormItemRule[]>> = reactive({
    // traffic_bus: [buildValidatorData({name: 'editorRequired', title: t('scenic.traffic_bus')})],
    // scenic_des: [buildValidatorData({name: 'editorRequired', title: t('scenic.scenic_des')})],
    // book_notice: [buildValidatorData({name: 'editorRequired', title: t('scenic.book_notice')})],
    name: [buildValidatorData({name: 'required', title: t('scenic.name')})],
    cate_ids: [buildValidatorData({name: 'required', title: "分类"})],
    limit_nums: [buildValidatorData({name: 'required', title: "每天限制数量"})],
    fifteen: [buildValidatorData({name: 'required', title: "15天内限制数量"})],
    normal_price: [buildValidatorData({name: 'required', title: "价格"})],
    province_name: [buildValidatorData({name: 'required', title: "省份"})],
    vip_price: [buildValidatorData({name: 'required', title: "会员价格"})],
    cover: [buildValidatorData({name: 'required', title: t('scenic.cover')})],
    banner: [buildValidatorData({name: 'required', title: t('scenic.banner')})],
    address: [buildValidatorData({name: 'required', title: t('scenic.address')})],
    longitude: [buildValidatorData({name: 'required', title: t('scenic.longitude')})],
    latitude: [buildValidatorData({name: 'required', title: t('scenic.latitude')})],
    create_time: [buildValidatorData({name: 'date', title: t('scenic.create_time')})],
    create_time: [buildValidatorData({name: 'date', title: t('scenic.create_time')})],
    city: [buildValidatorData({name: 'required', title: '所属城市'})],

})
</script>

<style scoped lang="scss"></style>
